package com.jiyun.controller;

import com.jiyun.dto.LunbotuDto;
import com.jiyun.model.xitong.Lunbotu;
import com.jiyun.oss.AliyunOSSUtils;
import com.jiyun.result.Result;
import com.jiyun.service.lunbotuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("lunbotu")
@CrossOrigin
@Api("轮播图管理")
public class lunbotuController {
    @Autowired
    private lunbotuService lunbotuService;
    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;
    @PostMapping("findAll")
    @ApiOperation("轮播图展示所有")
    public Result findAll(@RequestBody LunbotuDto lunbotuDto, Integer pn, Integer ps){
        return lunbotuService.findAll(lunbotuDto,pn,ps);
    }
    @RequestMapping("upload")
    @ApiOperation("轮播图上传")
    public String upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();
        String s = aliyunOSSUtils.putObject(inputStream, originalFilename);
        return s;
    }
    @PostMapping("save")
    @ApiOperation("轮播图处理")
    public void save(@RequestBody Lunbotu lunbotu){
        if(lunbotu.getId()==null){
            lunbotu.setFaburen("admin");
            lunbotuService.save(lunbotu);
        }else {
            lunbotuService.updateById(lunbotu);
        }
    }
    @GetMapping("de")
    @ApiOperation("轮播图删除")
    public void de(Integer id){
        lunbotuService.removeById(id);
    }
    @GetMapping("updateStatus")
    @ApiOperation("轮播图更新状态")
    public void updateStatus(Integer id){
        lunbotuService.updateStatus(id);
    }
}
